package core;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

import org.bouncycastle.asn1.*;

public class ConvertPdfToTxt {
	
	public ConvertPdfToTxt(){}
	
	public boolean isPDF(String pdfPath){
		try {
			PdfReader reader = new PdfReader(pdfPath);
			reader.getInfo();
		} catch (IOException e) {
			return false;
		}
		return true;
	}
	
	 public void parsePdf(String pdf, String txt) throws IOException {
	        PdfReader reader = new PdfReader(pdf);
	        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
	        FileOutputStream stream = new FileOutputStream(txt);
	        PrintWriter out = new PrintWriter(stream);
	        //PrintWriter out = new PrintWriter(new FileOutputStream(txt));
	        TextExtractionStrategy strategy;
	        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
	            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
	            out.println(strategy.getResultantText());
	        }
	        stream.flush();
	        stream.close();
	        out.flush();
	        out.close();
	        
	        reader.close();	  
	        
	      
	    }

}
